package org.eclipse.football;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OffPlayerServlet extends HttpServlet implements Servlet {
	// Override doPost function
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String button = request.getParameter("Off Player Button");
		
		// Obtain existing team
		Team team = (Team) request.getSession(true).getAttribute("team");
				
		// Obtain existing league
		League league = (League) request.getSession(true).getAttribute("league");
				
		// Declare an offensive  player variable
		OffPlayer player;
		
		if (button.equals("Add Offensive Player")){
			// Read in information submitted by form
			String qb = request.getParameter("qb");
			String rb = request.getParameter("rb");
			String wr = request.getParameter("wr");
			String te = request.getParameter("te");
			String name = null;
			String team_name = null;
//			System.out.println("Players are: _______________________________________________");
//			System.out.println(qb);
//			System.out.println(rb);
//			System.out.println(wr);
//			System.out.println(te);
		
			if (qb != null && !(qb.equals("choose QB,none"))){
				name = qb.split(",")[0];
				team_name = qb.split(",")[1];
			}
			if (rb != null && !(rb.equals("choose RB,none"))){
				name = rb.split(",")[0];
				team_name = rb.split(",")[1];
			}
			if (wr != null && !(wr.equals("choose WR,none"))){
				name = wr.split(",")[0];
				team_name = wr.split(",")[1];
			}
			if (te != null && !(te.equals("choose TE,none"))){
				name = te.split(",")[0];
				team_name = te.split(",")[1];
			}
//			System.out.println("The player picked is "+name);
		
			// Make sure that new players name is not null or blank and team is not null
			if(name != null && !name.equalsIgnoreCase("") && team != null){
				// If not then create a player with the given name and team
				player = new OffPlayer(name, team_name);
		
				// Add the new player to this team
				team.addOffPlayer(player);
			}
		}
		else if (button.equals("Fire!")){
			String name = request.getParameter("name");
			String team_name = request.getParameter("team");
			
			if(name != null && !name.equalsIgnoreCase("") && team != null
					&& team_name != null && !team_name.equalsIgnoreCase("")){
				player = new OffPlayer(name, team_name);
				team.removeOffPlayer(player);
			}
		}
		
		// Write the current state of the league out to a file
		league.writeState("league.txt");
		
		// Set league in the session
		request.getSession().setAttribute("league", league);
		
		// Set team in the session
		request.getSession().setAttribute("team", team);

		// Set all 3 player types to null in the session
		request.getSession().setAttribute("off", null);
		request.getSession().setAttribute("def", null);
		request.getSession().setAttribute("kicker", null);
		
		// Forward control back to stats.jsp
		getServletContext().getRequestDispatcher("/TeamPage.jsp").forward(request, response);
		
		// TODO Auto-generated method stub
		//super.doPost(request, response);
	}

}

